﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DLL.Entities;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
using System.Timers;
using System.Threading;

namespace COMM
{
    public partial class Form1 : Form
    {
        delegate void SetTextboxesdelagate(string text, TextBox obj); 
        public Form1()
        {
            InitializeComponent();
        }
        Missile missile = new Missile();
        Targets target = new Targets();
        private void Form1_Load(object sender, EventArgs e)
        {
            Com info = new Com();
            TextReader reader = null;
            try
            {
                //Deserializes Com_Config.xml to get the Current Model and Version 
                reader = new StreamReader("../../XML/Com_Config.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(Com));
                info = (Com)serializer.Deserialize(reader);

                reader.Close();
                //Puts the info in the respects textboxes will either show online or offline.
                txtboxModel.Text = Convert.ToString(info.Model);
                txtboxVersion.Text = Convert.ToString(info.Version);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            //Creates a timer that will read the Status of both the MIS and Rad every 2 seconds. 
            System.Timers.Timer myTimer = new System.Timers.Timer(2100);
            myTimer.Elapsed += new ElapsedEventHandler(StatusEvent);
            myTimer.Enabled = true;

            //Creates a timer that will read the event information from both MIS and Rad every 2 seconds. 
            System.Timers.Timer Eventimer = new System.Timers.Timer(2000);
            myTimer.Elapsed += new ElapsedEventHandler(RadMisEvent);
            myTimer.Enabled = true;

            //Form load MIS_config
            missile.Model = "Destroyer 9000";
            missile.Version = 10;
            XmlSerializer serializer1 = new XmlSerializer(typeof(Missile));
            TextWriter textWriter = new StreamWriter("../../XML/Mis_Config.xml");
            serializer1.Serialize(textWriter, missile);
            textWriter.Close();

            //Form load COM missileqty
            target.MissileQty = 10;
            XmlSerializer serializer2 = new XmlSerializer(typeof(Targets));
            TextWriter textWriter1 = new StreamWriter("../../../COMM/XML/COM_Event.xml");
            serializer2.Serialize(textWriter1, target);
            textWriter1.Close();

        }

            private void StatusEvent(object source, ElapsedEventArgs e)
            {
                {
                Missile MISstatus = new Missile();
                TextReader Misreader = null;
                Radar Radarstat = new Radar();
                TextReader Radreader = null;
                try
                {
                    

                    //Deserializes MIS_Config.xml to get the status of the MIS system  
                    Misreader = new StreamReader("../../../MIS/XML/MIS_Status.xml");
                    XmlSerializer MISserializer = new XmlSerializer(typeof(Missile));
                    MISstatus = (Missile)MISserializer.Deserialize(Misreader);

                    Misreader.Close();
                    SetTextBoxes(Convert.ToString(MISstatus.MissileStatus), txtboxMissileStat);

                   
                    //Deserializes RAD_Status.xml to get the status of the RAD system  
                    Radreader = new StreamReader("../../../RAD/XML/RAD_Status.xml");
                    XmlSerializer Radserializer = new XmlSerializer(typeof(Radar));
                    Radarstat = (Radar)Radserializer.Deserialize(Radreader);

                    Radreader.Close();
                    SetTextBoxes(Convert.ToString(Radarstat.RadarStatus), txtboxRadStat);

                }


                catch (XmlException xe)
                {
                    MessageBox.Show(xe.Message, "XML Parse Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                }
            }

            private void RadMisEvent(object source, ElapsedEventArgs e)
           {
               {
                Missile MISqty = new Missile();
                TextReader Misreader = null;
                Targets Targetinfo = new Targets();
                TextReader Radreader = null;
                TextReader Comreader = null;
                try
                {

                    //Deserializes MIS_Event.xml to get the event info of the MIS system  
                    Misreader = new StreamReader("../../../MIS/XML/MIS_Event.xml");
                    XmlSerializer MISserializer = new XmlSerializer(typeof(Missile));
                    MISqty = (Missile)MISserializer.Deserialize(Misreader);

                    Misreader.Close();
                    //Reads from COM_Event to grab the missileqty to display on textbox
                    Comreader = new StreamReader("../../../COMM/XML/COM_Event.xml");
                    XmlSerializer T2serializer = new XmlSerializer(typeof(Targets));
                    target = (Targets)T2serializer.Deserialize(Comreader);

                    Comreader.Close();
                    //Sets the MissileQuantity textbox with the missiles remaining
                    SetTextBoxes(Convert.ToString(target.MissileQty), txtboxMissileremain);

                    Utility.CopyFile("MIS_Event.xml", "../../../MIS/XML/");
                    // Utility.DeleteFile("../../../MIS/XML/MIS_Event.xml"); 
                    //Deserializes RAD_Event.xml to get the event info of the RAD system  
                    Radreader = new StreamReader("../../../RAD/XML/RAD_Event.xml");
                    XmlSerializer Radserializer = new XmlSerializer(typeof(Targets));
                    Targetinfo = (Targets)Radserializer.Deserialize(Radreader);

                    Radreader.Close();
                    SetTextBoxes(Convert.ToString(Targetinfo.TargetName), txtboxTarget);
                    SetTextBoxes(Convert.ToString(Targetinfo.TargetDistance), txtboxRange);
                    SetTextBoxes(Convert.ToString(Targetinfo.TargetAltitude), txtboxAltitude);
                    SetTextBoxes(Convert.ToString(Targetinfo.TargetDirection), txtboxDirection);
                    SetTextBoxes(Convert.ToString(Targetinfo.TargetSpeed), txtboxSpeed);
                    SetTextBoxes(Convert.ToString(Targetinfo.TargetNav), txtboxHeading);
                    Utility.CopyFile("RAD_Event.xml","../../../RAD/XML/");


                    //Writes to COM_Event depending on COMM's textbox for MIS to read
                    target.TargetName = txtboxTarget.Text;
                    target.TargetDirection = Convert.ToInt32(txtboxDirection.Text);
                    target.TargetDistance = Convert.ToInt32(txtboxRange.Text);
                    target.TargetAltitude = Convert.ToInt32(txtboxAltitude.Text);
                    target.TargetSpeed = Convert.ToInt32(txtboxSpeed.Text);
                    target.TargetNav = txtboxHeading.Text;
                    XmlSerializer serializer = new XmlSerializer(typeof(Targets));
                    TextWriter tw = new StreamWriter("../../XML/COM_Event.xml");
                    serializer.Serialize(tw, target);
                    tw.Close();

                }


                catch (XmlException xe)
                {
                    MessageBox.Show(xe.Message, "XML Parse Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

         }

        private void SetTextBoxes(string text, TextBox obj)
        {
        // InvokeRequired required compares the thread ID of the
        // calling thread to the thread ID of the creating thread.
        // If these threads are different, it returns true.
            if (obj.InvokeRequired)
            {
                SetTextboxesdelagate d = new SetTextboxesdelagate(SetTextBoxes);
                this.Invoke(d, new object[] { text , obj });
            }
            else
            {
                obj.Text = text;
            }
        }

        private void btnFire_Click_1(object sender, EventArgs e)
        {
            //Decrements missile on fire clicked and writes it to the COM_event
            TextReader reader1 = null;
            try
            {
                reader1 = new StreamReader("../../../COMM/XML/COM_Event.xml");
                XmlSerializer T2serializer = new XmlSerializer(typeof(Targets));
                target = (Targets)T2serializer.Deserialize(reader1);

                reader1.Close();
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (target.MissileQty <= 0)
            {
                MessageBox.Show("Out of Missiles, must reload in the Missile System!", "Reload");
            }
            else
            {
                target.MissileQty -= 1;
            }
            XmlSerializer Tserializer = new XmlSerializer(typeof(Targets));
            TextWriter textWriter = new StreamWriter("../../../COMM/XML/COM_Event.xml");
            Tserializer.Serialize(textWriter, target);
            textWriter.Close();

            if (target.MissileQty >= 1)
            {
                MessageBox.Show("Target Destroyed!", "Fire");
            }
        }

        private void btnDestruct_Click(object sender, EventArgs e)
        {
            
            Targets T = new Targets();
            //Serializes Target object with Comm information and creates an event file called Com_Event 
            T.TargetName = txtboxTarget.Text;
            T.TargetDirection = Convert.ToInt32(txtboxDirection.Text);
            T.TargetDistance = Convert.ToInt32(txtboxRange.Text);
            T.TargetAltitude = Convert.ToInt32(txtboxAltitude.Text);
            T.TargetSpeed = Convert.ToInt32(txtboxSpeed.Text);
            T.TargetNav = txtboxHeading.Text;
            T.Fired = "No";
            XmlSerializer serializer = new XmlSerializer(typeof(Targets));
            TextWriter tw = new StreamWriter("../../XML/COM_Event.xml");
            serializer.Serialize(tw, T);
            tw.Close();

            if (target.MissileQty >= 1)
            {
                MessageBox.Show("Missile Destructed", "Destruct");
            }
           
         }   
            

       
    }
}
